//
//  ViewController.swift
//  XQMacRealmDemo
//
//  Created by WXQ on 2019/8/5.
//  Copyright © 2019 WXQ. All rights reserved.
//

import Cocoa
import Realm

class ViewController: NSViewController {

    @IBOutlet weak var xq_idTF: NSTextField!
    
    var sModel: XQStudentModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    @IBAction func respondsToDeleteAll(_ sender: Any) {
        XQStudentRealmTool.deleteAll()
    }
    
    @IBAction func respondsToQuery(_ sender: Any) {
        let result = XQStudentRealmTool.getAllStudent()
        print("查询: ", result?.count)
        
        
        let bookResult = XQStudentRealmTool.getAllBook()
        if let bookResult = bookResult {
            
            for index in 0..<bookResult.count {
                let item = bookResult[index]
                // 为什么一直取不出来属性...
                let bModel = item
                self.xq_idTF.stringValue = bModel.name
                
                print("书: ", item)
                self.view.window?.title = "asd" + item.name
                for student in item.owners {
                    print("持有 " + item.name + " 的人: ", student)
                    
                    let str = item.name + ": asd :" + student.name
                    print("name: ", str)
                }
            }
        }
        
    }
    
    @IBAction func respondsToQueryId(_ sender: Any) {
        let model = XQStudentRealmTool.getStudent(self.xq_idTF.stringValue)
        if let model = model {
            print("查询: ", model, model.name)
            self.sModel = model
        }else {
            print("查询: 无结果")
        }
        
        let model1 = XQStudentRealmTool.getStudent(withName: "wxq")
        if let model = model1 {
            for item in model {
                print("查询 name : ", item, item.name)
            }
            print("查询 name 结束")
        }else {
            print("查询 name : 无结果")
        }
    }
    
    @IBAction func respondsToAdd(_ sender: Any) {
        print("写入")
        
        DispatchQueue.init(label: "awd").async {
            let student = XQStudentModel()
            student.name = "asd"
            student.age = 19
            student.weight = 180
            student.swiftBirthday = Date.init()
            
            XQStudentRealmTool.insertStudent(by: student)
        }
        
        let student = XQStudentModel()
        student.name = "wxq"
        student.age = 19
        student.weight = 180
        student.swiftBirthday = Date.init()
        
        let book = XQBookModel.init()
        book.name = "语文书"
        book.author = "语文"
        
        student.books.append(book)
        
        XQStudentRealmTool.insertStudent(by: student)
        
        DispatchQueue.init(label: "awd3").async {
            let student = XQStudentModel()
            student.name = "asd"
            student.age = 19
            student.weight = 180
            student.swiftBirthday = Date.init()
            
            XQStudentRealmTool.insertStudent(by: student)
        }
        
        DispatchQueue.init(label: "awd").async {
            let student = XQStudentModel()
            student.name = "asd"
            student.age = 19
            student.weight = 180
            student.swiftBirthday = Date.init()
            
            XQStudentRealmTool.insertStudent(by: student)
        }
        
        DispatchQueue.init(label: "awd3").async {
            let student = XQStudentModel()
            student.name = "asd"
            student.age = 19
            student.weight = 180
            student.swiftBirthday = Date.init()
            
            XQStudentRealmTool.insertStudent(by: student)
        }
        
    }
    
    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

